package com.work.formaldehyde.activity;

import android.app.ActivityOptions;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.umeng.analytics.MobclickAgent;
import com.work.formaldehyde.R;
import com.work.formaldehyde.model.UserModel;
import com.work.formaldehyde.util.ApiUtils;
import com.work.formaldehyde.util.DataCleanManager;
import com.work.formaldehyde.util.Url;
import com.work.formaldehyde.view.RoundImageView;

public class SetActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView jiance_comeback;
    private LinearLayout shdz, xgmm, sjhm, zfb, wx, tz, qchc, gywm, esc;
    private TextView zfbbd, wxbd, sjhbd, qkhcbd;
    private ProgressBar pb_main_download;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ApiUtils.ActivityIsLogin(this, 1);
        setContentView(R.layout.activity_set);
        jiance_comeback = findViewById(R.id.jiance_comeback);
        jiance_comeback.setOnClickListener(this);
        if (ApiUtils.isNetworkConnected(SetActivity.this))
            inti();
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (!Url.USER_WECHAT_NAME.equals(""))
            wxbd.setText("已绑定");
        if (!Url.USER_ALIPAY_NAME.equals(""))
            zfbbd.setText("已绑定");
        if (!Url.USER_PHONE_NAME.equals(""))
            sjhbd.setText("已绑定");
    }

    /**
     * 初始化加载调用方法
     */
    public void inti() {
        pb_main_download = findViewById(R.id.pb_main_download);
        qkhcbd = findViewById(R.id.qkhcbd);
        sjhbd = findViewById(R.id.sjhbd);
        zfbbd = findViewById(R.id.zfbbd);
        wxbd = findViewById(R.id.wxbd);
        esc = findViewById(R.id.esc);
        String getnum = getCacheSize();
        if (Url.USER_ID.equals(""))
            esc.setVisibility(View.GONE);
        else
            esc.setVisibility(View.VISIBLE);
        qkhcbd.setText(getnum);
        esc.setOnClickListener(this);
        if (Url.USER_WECHAT_NAME.equals("")) {
            wxbd.setText("未绑定");
        } else {
            wxbd.setText("已绑定");
        }
        if (Url.USER_ALIPAY_NAME.equals("")) {
            zfbbd.setText("未绑定");
        } else {
            zfbbd.setText("已绑定");
        }
        if (Url.USER_PHONE_NAME.equals("")) {////
            sjhbd.setText("未绑定");
        } else {
            sjhbd.setText("已绑定");
        }
        shdz = findViewById(R.id.shdz);
        xgmm = findViewById(R.id.xgmm);
        sjhm = findViewById(R.id.sjhm);
        zfb = findViewById(R.id.zfb);
        wx = findViewById(R.id.wx);
        tz = findViewById(R.id.tz);
        qchc = findViewById(R.id.qchc);
        gywm = findViewById(R.id.gywm);
        shdz.setOnClickListener(this);
        xgmm.setOnClickListener(this);
        sjhm.setOnClickListener(this);
        zfb.setOnClickListener(this);
        wx.setOnClickListener(this);
        tz.setOnClickListener(this);
        qchc.setOnClickListener(this);
        gywm.setOnClickListener(this);
        user_img = findViewById(R.id.user_img);
        user_name = findViewById(R.id.user_name);
        user_id = findViewById(R.id.user_id);
        if (!Url.USER_HEAD_IMG.equals(""))
            Glide.with(SetActivity.this)
                    .load(Url.USER_HEAD_IMG)
                    .into(user_img);
        if (!Url.USER_NAME.equals(""))
            user_name.setText(Url.USER_NAME);
        if (!Url.USER_PHONE_NAME.equals(""))
            user_id.setText(Url.USER_PHONE_NAME);
    }

    private RoundImageView user_img;
    private TextView user_name, user_id;

    private int intis = 0;

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.jiance_comeback:
                onBackPressed();
                break;
            case R.id.shdz:
                Intent toaddress = new Intent(SetActivity.this, GetAddressActivity.class);
                startActivity(toaddress, ActivityOptions.makeSceneTransitionAnimation(SetActivity.this).toBundle());
                break;
            case R.id.xgmm:

                break;
            case R.id.sjhm:
                if (!sjhbd.getText().equals("已绑定")) {
                    Intent setdh = new Intent(SetActivity.this, SetPhoneActivity.class);
                    startActivity(setdh, ActivityOptions.makeSceneTransitionAnimation(SetActivity.this).toBundle());
                }
                break;
            case R.id.zfb:
                if (!zfbbd.getText().equals("已绑定")) {
                    Intent setzfb = new Intent(SetActivity.this, SetZhiFuBaoActivity.class);
                    startActivity(setzfb, ActivityOptions.makeSceneTransitionAnimation(SetActivity.this).toBundle());
                }
                break;
            case R.id.wx:
                if (!wxbd.getText().equals("已绑定")) {
                    Intent setwx = new Intent(SetActivity.this, SetWechatActivity.class);
                    startActivity(setwx, ActivityOptions.makeSceneTransitionAnimation(SetActivity.this).toBundle());
                }
                break;
            case R.id.esc:
                SharedPreferences mSharedPreferences = getSharedPreferences("user", MODE_PRIVATE);  //名称和类型
                SharedPreferences.Editor mEditor = mSharedPreferences.edit();
                mEditor.putString("user_id", "");
                Url.USER_ID = "";
                mEditor.putString("user_name", "");
                Url.USER_NAME = "";
                mEditor.putString("user_pwd", "");
                mEditor.putString("user_phone", "");
                Url.USER_PHONE_NAME = "";
                mEditor.putString("user_wechat", "");
                Url.USER_WECHAT_NAME = "";
                mEditor.putString("user_wechatpwd", "");
                mEditor.putString("user_zfb_zh", "");
                Url.USER_ALIPAY_NAME = "";
                mEditor.putString("user_zfb_name", "");
                Url.USER_ALIPAY_USER_NAME = "";
                mEditor.putString("user_nickname", "");
                Url.USER_NICK_NAME = "";
                ApiUtils.SetLog("user_head=" + "");
                mEditor.putString("user_head", "");
                Url.USER_HEAD_IMG = "";
                Url.MORENDIZHI = "";
                Url.MORENNAME = "";
                Url.MORENPHONE = "";
                mEditor.apply();
                mEditor.commit();
                onBackPressed();
                break;
            case R.id.qchc:
                if (intis == 0) {
                    show();
                } else
                    ApiUtils.SetToast(SetActivity.this, "无需清理！");
                break;
            case R.id.gywm:
                Intent ckgd = new Intent(SetActivity.this, GuanYuWoMenActivity.class);
                startActivity(ckgd, ActivityOptions.makeSceneTransitionAnimation(SetActivity.this).toBundle());
                break;
        }
    }

    private AlertDialog.Builder ibuilder;

    private void show() {
        ibuilder = new AlertDialog.Builder(SetActivity.this);
        ibuilder.setTitle("清除缓存");
        ibuilder.setMessage("清除缓存会导致下载的内容删除，是否确定?");
        ibuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        ibuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                intis++;
                DataCleanManager.clearAllCache(SetActivity.this);
                pb_main_download.setVisibility(View.VISIBLE);
                qkhcbd.setVisibility(View.GONE);
                dialog.dismiss();
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        String getnum = getCacheSize();
                        intis = 0;
                        qkhcbd.setText(getnum);
                        qkhcbd.setVisibility(View.VISIBLE);
                        pb_main_download.setVisibility(View.GONE);
                    }
                }, 3000);
            }
        });

        Dialog dialog = ibuilder.create();
        dialog.show();
        WindowManager m = getWindowManager();
        Display d = m.getDefaultDisplay();  //为获取屏幕宽、高
        android.view.WindowManager.LayoutParams p =
                dialog.getWindow().getAttributes();  //获取对话框当前的参数值
        p.width = (int) (d.getWidth() * 0.8);    //宽度设置为屏幕的0.9
        dialog.getWindow().setAttributes(p);    //设置生效
    }

    private String getCacheSize() {
        String str = "";
        try {
            str = DataCleanManager.getTotalCacheSize(SetActivity.this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }
}
